<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script src="{{ getAssets }}js/ajaxfileupload.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : "{{ urlfor "ScreeningController.Imports"}}"
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData)
			$('#search_key').val() ? params.Filter = $('#search_key').val() : ''
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"mDataProp" : "Id", "bSortable": false, render:function(data, type, full, meta) {
				return '<input type="checkbox" class="am-checkbox" name="ids[]" value="'+ full.Id +'" />';
			}}
			,{"mDataProp" : "Id", "bSortable": false}
			,{"mDataProp" : "SerialNumber", "bSortable": false}
			,{"data" : "Mobile","bSortable": false}
			,{"mDataProp" : "Realname", "bSortable": false}
			,{"mDataProp" : "Sex", "bSortable": false}
			,{"mDataProp" : "Age", "bSortable": false}
			,{"mDataProp" : "Birthday", "bSortable": false}
			,{"mDataProp" : "SchoolName", "bSortable": false}
			,{"mDataProp" : "Classes", "bSortable": false}
			,{"mDataProp": "CreatedAt", render:function(data, type, full, meta) {
				return timetrans(data);	
			}} 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : false
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : false //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
	});
	
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
	});
	
	$('#selectAll').click(function(){
		if($(this).prop('checked')) {
			$('input[name="ids[]"]').each(function(){
				if($(this).prop('checked')) {
					$(this).prop('checked', false)	
				}else {
					$(this).prop('checked', true)		
				}
			})
		} else {
			$('input[name="ids[]"]').prop('checked', false)	
		}
	});
	$('#deleteData').click(function(){
		
		if($('input[name="ids[]"]:checked').length <= 0) {
			layer.alert("请选择需要删除的记录！");
			return false;	
		}
		
		var ids = "0";
		$('input[name="ids[]"]:checked').each(function(index, element) {
            ids += ","+$(this).val();
        });
		
		layer.confirm('确定要删除所选记录吗？删除后不可恢复！', function(index){
			$.post('{{ urlfor "ScreeningController.DeleteImports"}}', {ids:ids}, function(res){
				if(res.code == 1) {
					layer.msg(res.msg)
				} else {
					tables.ajax.reload();
				}
				layer.close(index);
			}, 'json');
		});
	});	
	
	$('#import').click(function(){
		layer.open({
            type: 1,
            title: "导入数据",
            shadeClose: false,
            shade: 0,
            area: ['600px', '400px'],
            content: $('#importHtml')
        });
		$('select').trigger('changed.selected.amui');
	});
	
	$("#checkUploadSubmit").validator({
		submit: function() {
			if(this.isFormValid()) {
				$('#checkUploadSubmit').val("导入中...").attr("disabled", true);
				updloadCtrolInit();
			}
			return false;
		}
	});
	
	$('#inputImageUrl').on('change', function() {
      var fileNames = '';
      $.each(this.files, function() {
        fileNames += '<span class="am-badge">' + this.name + '</span> ';
      });
      $('#file-list').html(fileNames);
    });
	
    function updloadCtrolInit() {
		
		xsrf = $('meta[name="csrf-token"]').attr('content')
		SchoolId = $('#SchoolId').val();
		SchoolName = $('[name="SchoolId"] option:selected').text()
		
		var $this = $('#inputImageUrl');
		var fileinput = $("input[type='file']", this);
		// file 控件的id
		var inputId = $this.attr('id');
	
		//获取隐藏用于保存图片地址的input
		$.ajaxFileUpload({
			url: '{{ urlfor "ScreeningController.UploadImports" }}',
			secureuri: false,
			fileElementId: inputId,
			type: 'post',   //当要提交自定义参数时，这个参数要设置成post
			dataType: 'json',
			data: { type: 1 ,'_xsrf': xsrf, 'SchoolName' : SchoolName, 'SchoolId': SchoolId }, //用来传file其他的值
			success: function (re, status) {
				if (re.code === 0) {
					layer.msg("数据导入成功！");
					setTimeout(function(){
						layer.closeAll()	
						$("#searchsubmit").click()
					}, 2000)
				}else {
					layer.alert(re.msg, { icon: 2, title: '错误' });
				}
			},
			error: function (XHR, status, e) {              
			}
		});

    }	
</script>
